module.exports = class extends baseLogic {
    constructor() {
        super();
    }

    static init() {
        return new this();
    }

    async getItems(moduleId,userInfo) {
        let ret={};
        ret.orgItems= await  this.func.orgStruct.GetUserOrgStructItems(moduleId, userInfo);

        ret.PlatformItems = await this.DB.iframe_platform.Access.findAll({
            attributes: ['IP_ID', 'IP_Name'],
            where: {
                IP_IsDel: 0
            },
            raw:true
        });
        ret.PlatformRuleItems = await this.DB.iframe_platform_rule.Access.findAll({
            attributes: ['RuleID', 'RuleName', 'RulePlatformID'],
            where: {
                IsDel: 0
            },
            raw:true
        });
        return ret;
    }

    async insertOrUpdate(Model,userInfo) {
        let ret={};
        if (Model.Type && Model.Type === "Edit") {
            ret = await this.DB.iframe_orgstruct.Access.update({
                OS_Name: Model.OS_Name,
                OS_Type: Model.OS_Type,
                OS_Admin_UserID:Model.OS_Admin_UserID,
                OS_Addr:Model.OS_Addr,
                OS_Desc:Model.OS_Desc,
                OS_PlatformRuleID:Model.OS_PlatformRuleID,
                OS_PlatFormID:Model.OS_PlatFormID,
                OS_AreaCode:Model.OS_AreaCode
            }, {
                where: {
                    OS_ID: Model.OS_ID
                }
            })

        } else if (Model.Type && Model.Type == "Add") { //添加子节点
            var MaxSort = await this.DB.iframe_orgstruct.Access.max("OS_Sort", {
                where: {
                    OS_ParentID: Model.OS_ParentID
                }
            });
            if (isNaN(MaxSort)) {
                MaxSort = 0;
            } else {
                MaxSort += 1;
            }

            Model.OS_Sort = MaxSort;

            Model.OS_CreateTime = this.moment().format('YYYY-MM-DD HH:mm:ss');
            Model = await this.DB.iframe_orgstruct.Access.create(Model);
            await this.CheckIsParent_SystemOrg(Model.OS_ParentID);
            if(Model.OS_Type==="001"){
                let adminUser = {
                    UserID:this.webTools.myuuid.createUUID(),
                    UserName: 'admin:' + Model.OS_ID,
                    UserRealName: '企业超级管理员',
                    UserJobNum: '0000000',
                    UserPwd: this.webTools.common.GetMD5('123456'),
                    UserMobilePhone: '',
                    UserRule: '',
                    UserLoginIp: '',
                    UserPingYin: 'cjgly',
                    UserCreatetime: this.moment().unix(),
                    UserLoginTime: this.moment().unix(),
                    UserIsSuper: 1,
                    UserIsOnline: 0,
                    UserIsDel: 0,
                    UserAccessToken: '',
                    UserPartnersID: Model.OS_ID,
                    UserOrgStructID:Model.OS_ID,
                    UserHeaderImg:'/res/images/header/default.png'

                };
                await this.DB.iframe_user.Access.create(adminUser);
            }

        }
        return Model;
    }

    async del(model) {
        let ids = model.AllIDs.split(',');
        for (var i = 0; i < ids.length; i++) {
            let id = ids[i];
            if (ids[i] && id.length > 0) {

                await this.DB.iframe_orgstruct.Access.update({
                    OS_IsDel: 1
                }, {
                    where: {
                        OS_ID: id
                    }
                });
            }
        }
        await this.CheckIsParent_SystemOrg(model.CurNodeParentID);
        return true;
    }

    async move(model) {
        let TargetNodeChildrensIDs_array = (model.TargetNodeChildrensIDs + ',').split(',');
        if (model.CurNodeID) {
            await this.DB.iframe_orgstruct.Access.update({
                OS_ParentID: model.TargetParentID
            }, {
                where: {
                    OS_ID: model.CurNodeID
                }
            })
        }
        if (TargetNodeChildrensIDs_array && TargetNodeChildrensIDs_array instanceof Array && TargetNodeChildrensIDs_array.length > 0) {
            for (var i = 0; i < TargetNodeChildrensIDs_array.length; i++) {
                let id = TargetNodeChildrensIDs_array[i];
                if (id && Number(id) > 0) {
                    await this.DB.iframe_orgstruct.Access.update({
                        OS_Sort: i
                    }, {
                        where: {
                            OS_ID: id
                        }
                    })
                }
            }
        }
        await this.CheckIsParent_SystemOrg(model.TargetParentID);
        await this.CheckIsParent_SystemOrg(model.old_parent);
        return true;
    }

    async syncOrg() {
        let ret = await this.func.orgStruct.Sync();
        return ret;
    }

    async getItemsByPartnersId(queryModel,curUserInfo){
        let OrgStruct={};
        if(curUserInfo.IsShowAll){
            OrgStruct =await this.func.orgStruct.GetTreeData(queryModel.partnersId,'');
        }else{
            OrgStruct =await this.func.orgStruct.GetTreeData(UserInfo.UserPartnersID,queryModel.parentId);
        }
        return OrgStruct;
    }

    /**
     * 获取企业列表的树形结构
     * @returns {Promise<Array>}
     */
    async getPartnersTreeData(moduleId,userInfo){
        let ret= await this.func.orgStruct.getPartnersItemsTreeData(moduleId,userInfo);
        return ret;
    }

    /**
     * 检测是否是父级
     */
    async CheckIsParent_SystemOrg(ParentID) {
        if (ParentID === 0) {
            return false;
        };
        let count = await this.DB.iframe_orgstruct.Access.count({
            where: {
                OS_ParentID: ParentID,
                OS_IsDel: 0
            }
        });
        if (count == 0) {
            await this.DB.iframe_orgstruct.Access.update({
                OS_IsParent: 0
            }, {
                where: {
                    OS_ID: ParentID
                }
            });
        } else {
            await this.DB.iframe_orgstruct.Access.update({
                OS_IsParent: 1
            }, {
                where: {
                    OS_ID: ParentID
                }
            });
        }
        return true;

    }

};
